package cloud.common.redis.utils;

import cloud.common.redis.redis.service.impl.RedisHashServiceImpl;
import cloud.common.redis.redis.service.impl.RedisListServiceImpl;
import cloud.common.redis.redis.service.impl.RedisServiceImpl;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Arrays;
import java.util.Map;

/**
 * 公共注册类
 * @author czq
 */
public abstract class BaseClassListClientsRegistrar implements ImportBeanDefinitionRegistrar{
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        Class[] classes = this.getClassList();
        if (classes==null || classes.length==0){
            return;
        }
        String packageName = classes[0].getPackage().getName();
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
        scanner.scan(packageName);
        //循环注册bean
//        Class[] classes = this.getClassList();
//        Arrays.stream(classes).forEach(cls->{
//            //不包含bean则注册
//            if (!beanDefinitionRegistry.containsBeanDefinition(cls.getName())){
//                BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(cls);
//                BeanDefinition beanDefinition = builder.getBeanDefinition();
//                beanDefinitionRegistry.registerBeanDefinition(cls.getName(),beanDefinition);
//            }
//        });
//        this.registerClientConfiguration(annotationMetadata,beanDefinitionRegistry);


    }

    /**
     * 注册配置
     */
    protected void registerClientConfiguration(AnnotationMetadata metadata, BeanDefinitionRegistry registry){
        Class[] classes = this .getConfigList();
        if (classes==null || classes.length==0){
            return;
        }
        for (int i=0;i<classes.length;i++){
            Class cls = classes[i];
            Map<String, Object> defaultAttrs = metadata.getAnnotationAttributes(cls.getName(), true);
            if (defaultAttrs != null && defaultAttrs.containsKey("defaultConfiguration")) {

            }
            String name;
            if (metadata.hasEnclosingClass()) {
                name = "default." + metadata.getEnclosingClassName();
            } else {
                name = "default." + metadata.getClassName();
            }

            this.registerClientConfiguration(registry, name, cls,cls);
        }
//        Arrays.stream(classes).forEach(cls->{
//        });
    }

    /**
     * 注册
     * @param registry
     * @param name
     * @param configuration
     */
    private void registerClientConfiguration(BeanDefinitionRegistry registry, Object name, Object configuration,Class cls) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(cls);
        builder.addConstructorArgValue(name);
        builder.addConstructorArgValue(configuration);
        registry.registerBeanDefinition(name + "." + cls.getSimpleName(), builder.getBeanDefinition());
    }

    /**
     * 获取类列表
     * @return
     */
    protected abstract Class[] getClassList();

    /**
     * 获取配置
     * @return
     */
    protected abstract Class[] getConfigList();
}
